<template>
  <div class="app-container">
    <!-- 查询 -->
    <tl-list-search ref="search" :query-param="queryParam" @search="doQuery" :isDisplayDefaultSearch="false" v-if="isHeaderDisplay">
      <template slot="search">
        <el-form-item label="项目信息">
          <el-input style="width:180px" v-model="queryParam.qry_project" placeholder="项目名称关键词或编号" @keyup.enter.native="doQuery"></el-input>
        </el-form-item>
        <el-form-item label="项目级别">
          <el-select v-model="projectlevel" multiple clearable placeholder="请选择项目级别" style="width:190px" @change="doQueryConcatProjectLevel">
            <el-option v-for="item in codeModel.projectlevelList" :key="item.code" :label="item.name" :value="item.code"> </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="项目状态">
          <el-select v-model="projectlargestate" placeholder="项目状态" filterable multiple style="width:250px">
            <el-option v-for="item in codeModel.projectlargestateList" :key="item.code" :label="item.name" :value="item.code" />
          </el-select>
        </el-form-item>
      </template>
      <template slot="button">
        <el-button type="primary" icon="el-icon-zoom-in" @click="openDeatailSearchDialog">详细查询</el-button>
      </template>
      <template slot="button">
        <el-button type="primary" icon="el-icon-delete" @click="resetQuery">重置</el-button>
      </template>
    </tl-list-search>
    <!-- 工具栏 -->
    <tl-list-toolbar ref="toolbar" :refMainTable="$refs.mainTable" currentModuleTableCode="gpmProject/track" :startColumn="3" @add="openAddDialogTrack('editForm', 'before')" @exportExcel="doExportExcel" @deleteBatch="doDeleteBatch" @refresh="doRefresh" @columnCustomize="doColumnCustomize" @displaySearch="doDisplaySearch" :disableDeleteBatch="selectedRowKeys.length <= 0" :isDisplayDeleteBatch="false" :isDisplayAdd="false">
      <template slot="left-btn">
          <el-button type="primary" icon="el-icon-plus" @click="openAddProcessProject('editForm')">新增流程建设项目</el-button>
      </template>
    </tl-list-toolbar>
    <div>
      <!-- 列表选择 -->
      <div class="table-alert-div" ref="selector">
        <el-alert type="success" :closable="false" class="tl-table-alert-info" effect="light">
          <template slot="title">
            已选择&nbsp;<a class="tl-table-alert-info-a1">{{ selectedRowKeys.length }}</a
            >项&nbsp;&nbsp;
            <a class="tl-table-alert-info-a2" @click="doClearSelected">全部清空</a>
          </template>
        </el-alert>
      </div>
      <!--表格渲染-->
      <el-table class="tl-table" ref="mainTable" :row-style="tableRowStyle" :height="tableHeight" resizable-key="tableHeight" :resizable-refs="tableResizeRefs" v-loading="loading" :data="datas" @selection-change="doSelectChange" @sort-change="doSortChange" @row-dblclick="doRowDbClick" @row-click="doRowClick" row-key="id" border fit :row-class-name="doChangeRowClassName">
        <el-table-column type="selection" width="55px" fixed="left" :selectable="doDisableCheckBox"></el-table-column>
        <el-table-column type="index" label="序号" width="55px" align="center"></el-table-column>
        <el-table-column prop="whetherhistorydata" label="是否补录" width="100px" sortable="custom" sort-by="whetherhistorydata" align="center" header-align="center" :show-overflow-tooltip="true">
          <template v-slot="scope">
            <template v-if="scope.row.whetherhistorydata === '1'">是</template>
            <template v-else>否</template>
          </template>
        </el-table-column>
        <el-table-column prop="projectnumber" label="项目编号" width="230px" sortable="custom" sort-by="projectnumber" align="center" header-align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="projectmanager" label="项目经理" width="120px" sortable="custom" sort-by="projectmanager" align="center" header-align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="dispprojectstate" label="项目阶段" width="115px" sortable="custom" sort-by="projectstate" align="center" header-align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="dispprojectlevel" label="项目级别" width="100px" sortable="custom" sort-by="projectlevel" align="center" header-align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="projectname" label="项目名称" width="200px" sortable="custom" sort-by="projectname" align="left" header-align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="bidcycle" label="项目周期(天)" width="110px" sortable="custom" sort-by="bidcycle" align="center" header-align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="planstartdate" label="计划开始日期" width="120px" sortable="custom" sort-by="planstartdate" align="center" header-align="center" :formatter="dateFormat" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="planenddate" label="计划结束日期" width="120px" sortable="custom" sort-by="planenddate" align="center" header-align="center" :formatter="dateFormat" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="finishdate" label="立项完成日期" width="120px" sortable="custom" sort-by="finishdate" align="center" header-align="center" :formatter="dateFormat" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="sponsor" label="Sponsor" width="120px" sortable="custom" sort-by="sponsor" align="center" header-align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="owner" label="Owner" width="120px" sortable="custom" sort-by="owner" align="center" header-align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="applydeptname" label="所属部门" width="280px" sortable="custom" sort-by="applydeptname" align="left" header-align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="description" label="备注" min-width="250px" align="left" header-align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column label="操作" width="100px" v-slot="scope" fixed="right">
          <el-dropdown trigger="hover">
            <el-button type="text" icon="el-icon-s-operation" style="margin-left:10px">更多<i class="el-icon-arrow-down el-icon--right"></i></el-button>
            <el-dropdown-menu>
              <el-dropdown-item icon="el-icon-edit" :disabled="scope.row.state !== 0 && scope.row.state !== 2" @click.native="openEditDialog('editForm', scope.row.id)">编辑</el-dropdown-item>
              <el-dropdown-item icon="el-icon-delete" :disabled="scope.row.state !== 0 && scope.row.state !== 2" @click.native="doDelete(scope.row.id)">删除</el-dropdown-item>
              <el-dropdown-item icon="el-icon-view" @click.native="openViewDialog('editForm', scope.row.id)">查看立项信息</el-dropdown-item>
              <el-dropdown-item icon="el-icon-plus" @click.native="openAddSuperviseDialog('editSupervise', scope.row.id)">新增督办</el-dropdown-item>
              <el-dropdown-item type="text" icon="el-icon-edit" :disabled="!scope.row.changeownerpermission" @click.native="openChangeOwnerDialog('changeOwnerForm', scope.row)">变更项目经理</el-dropdown-item>
              <el-dropdown-item v-if="(scope.row.open === null || scope.row.open !== 1) && (scope.row.projectstate === '2' || scope.row.projectstate === '8')  && whetherpmo" icon="el-icon-circle-plus-outline" @click.native="changePermission(scope.row.id)">公开</el-dropdown-item>
              <el-dropdown-item v-if="scope.row.open === 1 && (scope.row.projectstate === '2' || scope.row.projectstate === '8') && whetherpmo" icon="el-icon-remove-outline" @click.native="changePermission(scope.row.id)">取消公开</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-table-column>
      </el-table>
      <!-- 列表分页 -->
      <el-row class="pagebar" type="flex" ref="pager" align="bottom">
        <el-col :span="12" style="margin-bottom:5px;">
          <el-col style="background-color:#D80A1E;height: 20px;width:20px;margin-left:10px" :offset="0"> </el-col>
          <el-col class="projectStateContext" :offset="0">
            项目超期
          </el-col>
          <el-col style="background-color:#FF8C2E;height: 20px;width:20px" :offset="0"> </el-col>
          <el-col class="projectStateContext" :offset="0">
            里程碑超期
          </el-col>
          <el-col style="background-color:#0000FF;height: 20px;width:20px" :offset="0"> </el-col>
          <el-col class="projectStateContext" :offset="0">
            流程超期
          </el-col>
          <el-col style="background-color:#B8860B;height: 20px;width:20px" :offset="0"> </el-col>
          <el-col class="projectStateContext" :offset="0">
            任务超期
          </el-col>
        </el-col>
        <el-col :span="12">
          <el-pagination @size-change="doChangeSize" @current-change="doChangeIndex" :current-page="pageInfo.currentPage" :page-sizes="pageInfo.pageSizes" :total="pageInfo.totalCount" layout="total, sizes, prev, pager, next, jumper" background class="tl-table-pagination"> </el-pagination>
        </el-col>
      </el-row>
      <index-comp ref="indexComp" :projectcode="projectcode"></index-comp>
      <tl-edit ref="editForm" @refresh="doRefresh"></tl-edit>
      <change-owner ref="changeOwnerForm"></change-owner>
      <tl-edit-supervise ref="editSupervise" @refresh="doRefresh" :projectinfo="projectinfo"></tl-edit-supervise>
      <tl-detail-search ref="detailSearch" @resetQueryParam="resetQueryForDetailSearch" :codeModel="codeModel" :queryParams.sync="queryParam" :projectLevel.sync="projectlevel" :createDepart.sync="createdepart" @loadData="loadData" :doDisplayState="false"></tl-detail-search>
    </div>
  </div>
</template>

<script>
/** mixins */
import { mixin_list } from '@tapui/mixins/mixin_list'
/** 子组件 */
import indexComp from '../gpmProjectInfo/indexComp'
import changeOwner from "./component/changeOwner"
import tlEdit from './edit'
import tlDetailSearch from './component/DetailSearch'
import tlEditSupervise from '../gpmProjectSupervise/edit'
import { validateNull } from '@tapui/utils/validate'
/** 工具类 */
import { checkPermission } from '@tapui/utils/permission'
import { getAction, putAction, postAction, deleteAction } from '@tapui/utils/http'
export default {
  name: 'sdProjectTrack',
  mixins: [mixin_list],
  components: { indexComp, tlEdit, tlDetailSearch, tlEditSupervise, changeOwner },
  data() {
    return {
      description: 'SD项目管理-列表查询',
      urlPrefix: '/sd/project/',
      url: {
        queryCodeList: '/sd/project/dataCodeList',
        load: '/sd/project/load',
        queryIsPMO: '/sd/project/queryIsPMO',
        changePermission: '/sd/projectReview/changePermission'
      },
      // 排序参数
      sorterInfo: {
        sidx: 'abnormalstate desc,id ',
        sord: 'desc'
      },
      projectcode: '',
      projectlevel: [],
      createdepart: [],
      projectinfo: {},
      // 筛选参数
      filtersInfo: {
        qry_projectmanagermenu: '1'
      },
      projectlargestate: ['0', '1'],
      // 是否为PMO
      whetherpmo: false
    }
  },
  methods: {
    /** 打开新增对话框之前 */
    beforeOpenAddDialog(editForm, rowData) {
      this.$refs[editForm].isProcessProject = '0'
      return true
    },
    /** 打开新增对话框实现 */
    openAddProcessProject(editForm, rowData) {
      if (!this.beforeOpenAddDialog(editForm, rowData)) {
        return
      }
      this.$refs[editForm].isProcessProject = '1'
      this.$refs[editForm].isAdd = true
      this.$refs[editForm].visible = true
      this.$refs[editForm].disableEditSubmit = false
      this.$nextTick(() => {
        this.afterOpenAddDialog(editForm, rowData)
      })
    },
    openChangeOwnerDialog(editForm, item) {
      this.$refs[editForm].id = item.id
      this.$refs[editForm].isAdd = false
      this.$refs[editForm].visible = true
      this.$refs[editForm].disableEditSubmit = false
      this.$refs[editForm].model = item
      let that = this
      setTimeout(() => {
        that.clickRowkey = item.id
      }, 100)
    },
    // 部门名称多选框
    doQueryConcatCreateDepart(data) {
      if (data.length > 0) {
        this.queryParam.qry_createdepart = data
          .map((item) => {
            return "'" + item + "'"
          })
          .join(',')
      } else {
        this.queryParam.qry_createdepart = null
      }
    },
    // 项目级别多选框
    doQueryConcatProjectLevel(data) {
      if (data.length > 0) {
        this.queryParam.qry_projectlevel = data
          .map((item) => {
            return "'" + item + "'"
          })
          .join(',')
      } else {
        this.queryParam.qry_projectlevel = null
      }
    },
    // 重置查询条件(子组件调用)
    resetQueryForDetailSearch() {
      this.queryParam = {}
      this.param = {}
      this.projectlevel = []
      this.createdepart = []
      this.doQuery()
    },
    // 重置所有参数
    resetQuery() {
      this.queryParam = {}
      this.param = {}
      this.projectlevel = []
      this.createdepart = []
      this.$refs.detailSearch.resetQueryParam()
      this.doQuery()
    },
    // 打开详细查询页面
    openDeatailSearchDialog() {
      this.$refs.detailSearch.visible = true
    },
    tableRowStyle(row, index) {
      if (row.row.abnormalstate === 30) {
        return { 'background-color': 'rgb(235,75,75)', color: 'white' }
      } else if (row.row.abnormalstate === 20) {
        return { 'background-color': 'rgb(255,140,46)', color: 'white' }
      } else if (row.row.abnormalstate === 10) {
        return { 'background-color': 'rgb(184,134,11)', color: 'white' }
      } else if (row.row.abnormalstate === 15) {
        return { 'background-color': '#0000FF', color: 'white' }
      }
    },
    // 禁用表格前面的选择
    doDisableCheckBox(row, index) {
      if (row.state !== 0 && row.state !== 2) {
        return false
      } else {
        return true
      }
    },
    doRowDbClick(row, column, event) {
      // 如果项目未完成立项审批，则双击跳转到立项页面
      if (row.state === 0 || row.state === 2) {
        this.openEditDialog('editForm', row.id)
      } else if (row.state === 1) {
        this.openViewDialog('editForm', row.id)
      } else {
        // 审批完成跳转到详情页
        this.projectcode = row.projectcode
        this.$nextTick(() => {
          this.$refs.indexComp.id = row.id
          this.$refs.indexComp.visible = true
        })
      }
    },
    openAddSuperviseDialog(editForm, id) {
      getAction(this.url.load, { id: id }).then(res => {
        if (res.success) {
          this.projectinfo = res.result
          this.$refs[editForm].isAdd = true
          this.$refs[editForm].visible = true
          this.$refs[editForm].disableEditSubmit = false
        }
      })
    },
    beforeLoadData() {
      // 交付项目状态大类筛选
      this.queryParam.qry_projectlargestate = validateNull(this.projectlargestate) ? '0,1,2' : this.projectlargestate.join(',')
      return true
    },
    openAddDialogTrack(editForm, str) {
      if (str === 'after') {
        this.$refs[editForm].isAdditional = '1'
      }
      this.openAddDialog(editForm)
    },
    queryIsPMO() {
      getAction(this.url.queryIsPMO).then(res => {
        if (res.success) {
          this.whetherpmo = res.result.whetherpmo
        } else {
          this.whetherpmo = false
        }
      })
    },
    /** 修改项目是否公开的权限 **/
    changePermission(projectid) {
      putAction(this.url.changePermission, { projectid: projectid }).then((res) => {
        if (res.success) {
          this.$tap.message(res.message, 'success')
        } else {
          this.$tap.alert(res.message, 'warning')
        }
        this.doRefresh()
      })
    }
  },
  created() {
    // 判断用户是否经营管理部PMO，可以进行关注与留言
    this.queryIsPMO()
  }
}
</script>

<style scoped>
.app-container >>> .el-table tbody tr.hover-row > td {
  background-color: transparent !important;
}
.app-container {
  height: 100%;
  padding: 10px;
  margin: 12px;
  background: white;
  border-radius: 5px;
}
</style>
